<html ng-app="app">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link href="../../build/common/base.css" rel="stylesheet">
	<script type="text/javascript" src="../../build/common/base.js"></script>
	<script type="text/javascript" src="../../build/common/ngEdit.js"></script>

	<script type="text/javascript">
	var id = $.getParam("id");
	
	var app = angular.module('app', ['baseDirective']);
	app.controller("ctrl", [ '$scope', 'baseService','ArrayToolService', function($scope, baseService,ArrayToolService) {
		$scope.ArrayTool = ArrayToolService;
		$scope.isSaveEd = id;
		if(id){
			$("#account").attr("readonly","readonly");
		}
		
		if($scope.isSaveEd){
			$("#userPostList").attr("src","userPostList.html?userId="+id);
			$("#userRoleList").attr("src","userRoleList.html?userId="+id);
		}
		
		/**
		 * 数据初始化。
		 */
		$scope.init=function(){
			if(!$scope.data){
				$scope.data={status:1};
			}
		};
		
	 	$scope.selectRoleRelation = function(){
	 		if(!$scope.data.orgRelationList){
	 			$scope.data.orgRelationList = [];
	 		}
	 		var arr = [];
	 		var newList = [];
			for(var i=0,relation;relation = $scope.data.orgRelationList[i++];){
				if(relation.type == "userRole"){
					var object = {};
					object.id = relation.roleName;
					object.name = relation.roleId;
					arr.push(object);
				}else{
					newList.push(relation);
				}
			}
	 		
	 		CustUtil.openCustDialog("roleSelector",function(data,innerWindow){
				$scope.$apply(function(){
					$scope.data.orgRelationList = newList;
					
					//将重新选择的放入到List中
					for(var i = 0; i < data.length; i++) {
						var rights = {
							type :"userRole",
							roleId: data[i].id,
							roleName : data[i].name,
							};
						$scope.data.orgRelationList.push(rights);
					}
					});
			   $.Dialog.close(innerWindow);
			},arr);
	 	}
	 	
	 	$scope.selectPostRelation = function(){
	 		if(!$scope.data.orgRelationList){
	 			$scope.data.orgRelationList = [];
	 		}
	 		var arr = [];
	 		var newList = [];
			for(var i=0,relation;relation = $scope.data.orgRelationList[i++];){
				if(relation.type == "groupUserRole"){
					arr.push(relation);
				}else{
					newList.push(relation);
				}
			}
	 		CustUtil.openCustDialog("postSelector",function(data,innerWindow){
				$scope.$apply(function(){
					$scope.data.orgRelationList = newList;
					
					//将重新选择的放入到List中
					for(var i = 0; i < data.length; i++) {
						var rights = {
							type :"groupUserRole",
							groupId: data[i].groupId,
							roleId: data[i].roleId,
							postName : data[i].name,
							};
						$scope.data.orgRelationList.push(rights);
					}
					});
			   $.Dialog.close(innerWindow);
			},arr);
	 		
	 	}
	 	
	 	$scope.selectGroupRelation = function(){
	 		if(!$scope.data.orgRelationList){
	 			$scope.data.orgRelationList = [];
	 		}
	 		var arr = [];
	 		var newList = [];
			for(var i=0,relation;relation = $scope.data.orgRelationList[i++];){
				if(relation.type == "groupUser"){
					var object = {};
					object.id = relation.groupName;
					object.name = relation.groupId;
					arr.push(object);
				}else{
					newList.push(relation);
				}
			}
	 		
	 		CustUtil.openCustDialog("orgSelector",function(data,innerWindow){
				$scope.$apply(function(){
					$scope.data.orgRelationList = newList;
					
					//将重新选择的放入到List中
					for(var i = 0; i < data.length; i++) {
						var rights = {
							type :"groupUser",
							groupId: data[i].id,
							groupName : data[i].name,
							};
						$scope.data.orgRelationList.push(rights);
					}
					});
			   $.Dialog.close(innerWindow);
			},arr);
	 	}
	}]);
	</script>
</head>
<body ng-controller="ctrl" class="panel success">
	<!-- 顶部按钮 -->
	<div class="panel-heading">
		<a class="btn btn-primary fa-save" ng-model="data" use-default-after-save-event="true" ab-save="/org/user/save"> 保存 </a>
		<a href="javascript:void(0)" class="btn btn-sm btn-primary fa-reply" onclick="javascript:$.Dialog.close(window);"> 返回 </a>
	</div>
	<form name="form" method="post" ab-load="/org/user/get?id={id}"  ng-model="data">
		<table class="form-table" cellspacing="0">
			<tr>
				<th>
					<span>姓名:</span>
					<span class="required">*</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.fullname" ab-validate="{required:true,maxlength:765}" />
				</td>

			</tr>
			<tr>
				<th>
					<span>账号:</span>
					<span class="required">*</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.account" id="account" ab-validate="{required:true,maxlength:20}" />
				</td>
			</tr>

			<tr ng-if="data.id==null">
				<th>
					<span>密码:</span>
					<span class="required">*</span>
				</th>
				<td>
					<input class="form-control" type="password" ng-model="data.password" ab-validate="{required:true,maxlength:192}" />
				</td>
			</tr>
			<tr>
				<th>
					<span>邮箱:</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.email" ab-validate="{required:false,maxlength:192,email:true}" />
				</td>
			</tr>
			<tr>
				<th>
					<span>手机号码:</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.mobile" ab-validate="{maxlength:96,phonenumber:true}" />
				</td>
			</tr>
			<tr>
				<th>
					<span>微信号:</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.weixin" ab-validate="{maxlength:192}" />
				</td>
			</tr>
			<tr>
				<th>
					<span>地址:</span>
				</th>
				<td>
					<input class="form-control" type="text" ng-model="data.address" ab-validate="{required:false}" />
				</td>
			</tr>

			<tr>
				<th>
					<span>性别：</span>
				</th>
				<td>
					<select class="form-control" ng-model="data.sex">
						<option value="未知">未知</option>
						<option value="男">男</option>
						<option value="女">女</option>
					</select>
				</td>
			</tr>

			<tr>
				<th>
					<span>状态:</span>
					<span class="required">*</span>
				</th>
				<td>
					<select class="form-control" ng-model="data.status" ab-validate="{required:true}">
						<option value="0">禁用</option>
						<option value="1">启用</option>
					</select>
				</td>
			</tr>
			
			<tr>
				<th>
					<span>角色:</span>
					<span class="required">*</span>
				</th>
				<td>
					<span ng-repeat="relation in data.orgRelationList" ng-show="relation.type ==='userRole'" style="margin-right: 10px" class="label label-danger"> {{relation.roleName}}
					<button href="javascript:void(0)" class="btn btn-link btn-default btn-xs" ng-click="ArrayTool.remove(relation,data.orgRelationList)">×</button></span>
					<span class="btn  btn-sm btn-primary fa fa-search" ng-click="selectRoleRelation()">选择</span> 
				</td>
			</tr>
			<tr>
				<th>
					<span>组织:</span>
					<span class="required">*</span>
				</th>
				<td>
					<span ng-repeat="relation in data.orgRelationList" ng-show="relation.type ==='groupUser'" style="margin-right: 10px" class="label label-danger"> {{relation.groupName}}
					<button href="javascript:void(0)" class="btn btn-link btn-default btn-xs" ng-click="ArrayTool.remove(relation,data.orgRelationList)">×</button></span>
					<span class="btn  btn-sm btn-primary fa fa-search" ng-click="selectGroupRelation()" >选择</span> 
				</td>
			</tr>
			<tr>
				<th>
					<span>岗位:</span>
					<span class="required">*</span>
				</th>
				<td>
					<span ng-repeat="relation in data.orgRelationList|filter:{type:'groupUserRole'}" style="margin-right: 10px" class="label label-danger"> {{relation.postName}}
					<button href="javascript:void(0)" class="btn btn-link btn-default btn-xs" ng-click="ArrayTool.remove(relation,data.orgRelationList)">×</button></span>
					<span class="btn  btn-sm btn-primary fa fa-search" ng-click="selectPostRelation()" >选择</span> 暂不支持回显
				</td>
			</tr>
			<tr>
				<th>
					<span>创建时间:</span>
				</th>
				<td>{{data.createTime | date :'yyyy-MM-dd'}}</td>
			</tr>
		</table>
	</form>
</body>
</html>